function AddCube(x_max, y_max, z_max, x_min, y_min, z_min, ztype)
    table.insert(servertable["map"]["zones"], {
              x_max = x_max,
              y_max = y_max,
              z_max = z_max,
              x_min = x_min,
              y_min = y_min,
              z_min = z_min,
              ztype = ztype
           } )
end
 
function CheckPoint(x,y,z,zonetype)
    for _,Zone in ipairs(servertable["map"]["zones"]) do
       if (	x >= Zone.x_min && x <= Zone.x_max &&
			y >= Zone.y_min && y <= Zone.y_max &&
			z >= Zone.z_min && z <= Zone.z_max &&
			zonetype == Zone.ztype) then
            return true
		end
    end
    return false
 end 

local Zonetypes = {"StationBig","Station","BuildZone","Minefield"}
for var=1, 20, 1 do 
	local zonetype = "Sector" .. var
	table.insert(Zonetypes,zonetype)
end
 
function RegisterCubes()
	local entities = ents.FindByClass( "func_brush" )
	for _, ent in ipairs( entities ) do
		local name = ent:GetName()
		if table.HasValue(Zonetypes,name) then
		    local minimum,maximum = ent:WorldSpaceAABB() 
			if string.find(name,"Sector") then
				local pos1 = {}
				pos1.X = maximum.X -100 	
				pos1.Y = maximum.Y -100 	
				pos1.Z = maximum.Z -100 	
				AddLight(pos1,"255 0 0")
				pos1.X = minimum.X +100 		
				pos1.Y = minimum.Y +100 	
				pos1.Z = minimum.Z +100 	
				AddLight(pos1,"255 0 0")
				pos1.X = minimum.X +20  
				pos1.Y = maximum.Y -20
				pos1.Z = maximum.Z -20
				AddLight(pos1,"255 0 0")
				pos1.X = maximum.X -100 		
				pos1.Y = minimum.Y +100 	
				pos1.Z = maximum.Z -100 	
				AddLight(pos1,"255 0 0")
				pos1.X = maximum.X -100 		
				pos1.Y = maximum.Y -100 	
				pos1.Z = minimum.Z +100 	
				AddLight(pos1,"255 0 0")
				pos1.X = minimum.X +100 		
				pos1.Y = minimum.Y +100 	
				pos1.Z = maximum.Z -100
				AddLight(pos1,"255 0 0")
				pos1.X = minimum.X +100 		
				pos1.Y = maximum.Y -100 	
				pos1.Z = minimum.Z +100 	
				AddLight(pos1,"255 0 0")
				pos1.X = maximum.X -100 	
				pos1.Y = minimum.Y +100 	
				pos1.Z = minimum.Z +100 	
				AddLight(pos1,"255 0 0")
			end
			--Msg("Adding Zone: " .. name .. " X: " .. maximum.X .. " " .. minimum.X .. " Y: " .. maximum.Y .. " " .. minimum.Y .. " Z: " .. maximum.Z .. " ".. minimum.Z .. "\n")
			AddCube(maximum.X,maximum.Y,maximum.Z,minimum.X,minimum.Y,minimum.Z,name)
			
		else
			Msg(name)
		end
	end			
end

function AddLight(pos,color)
	if (color == "0 255 0") then
		--Msg("Automatical adding light at " .. pos.X .." " .. pos.Y .. " " .. pos.Z .. " " .. color .."\n")
	end
	pos = Vector(pos.X,pos.Y,pos.Z)
	local light = ents.Create("env_lightglow")
	light:SetPos(pos)
	light:SetKeyValue("targetname", "moo")
	light:SetKeyValue("rendercolor", color)
	light:SetKeyValue("VerticalGlowSize", "10")
	light:SetKeyValue("HorizontalGlowSize", "10")
	light:SetKeyValue("MinDist", "2")
	light:SetKeyValue("MaxDist", "5")
	light:Spawn()
end

function GM:InitPostEntity( )
	RegisterCubes()
end

function InBuildZone(pos)
	return CheckPoint(pos.x,pos.y,pos.z,"BuildZone")
end

local function LimitNoclip( pl ) 
	if (InBuildZone(pl:GetPos( )) ) then return end
	return false
end 
hook.Add("PlayerNoclip", "Limit", LimitNoclip); 